module RbNarcissus
  BEFORE_AFTER = false

  # Pass a chunk of Javascript with format-like replacement strings for extracting information
  # from the javascript tree.
  class MatchExpression < SearchableHash
    @@parser = JavascriptParser.new
    @@parser.parse_substitutes = true

    def initialize raw_match_expression
      match_expression = case raw_match_expression
      when String
        processed_match_expression_string = raw_match_expression.dup
        @@parser.parse(processed_match_expression_string).to_tree
      when Hash
        raw_match_expression
      else
        raise ArgumentError, 'MatchExpression must be kind of String or Hash.  See documentation for details on MatchExpression formatting.'
      end

      super match_expression
    end

  end

  # Contains a corpus of javascript against
  class Matcher < SearchableHash
    @@parser = JavascriptParser.new

    def matches
      capture_accumulator
    end

    def initialize(javascript_text)
      raise ArgumentError unless javascript_text.kind_of?(String)
      super @@parser.parse(javascript_text).to_tree
    end

    def / provided_match_expression
      self.capture_accumulator = []
      match_expression = provided_match_expression.kind_of?(MatchExpression) ?
        provided_match_expression :
        MatchExpression.new(provided_match_expression)

      self.capture_reset_accumulator.each do |n|
        n.capture = true
      end if self.capture_reset_accumulator

      match = match_expression
      super match
    end

  end

  class Javascript < Matcher
  end

end

class SearchableArray < Array
end

class SearchableHash < Hash
end
